<template>
	<view class="content">
		<door-navbar></door-navbar>
		<u-cell-group :border="false" :customStyle="{padding:'0 20rpx'}">
			<u-cell title="姓名" :titleStyle="{color:'#333',fontSize:'28rpx'}"
				:rightIconStyle="{color:'#333',width:'12rpx',height:'12rpx'}" @click="showNamePopup">
				<template slot="value">
					<view class="value">{{userInfo.userName}}</view>
				</template>
				<template slot="right-icon">
					<u-icon style="margin-left: 20rpx;" size="24" name="arrow-right" color="#333">
					</u-icon>
				</template>
			</u-cell>
		</u-cell-group>
		<u-popup :show="showPopup" overlayOpacity='0' closeIconPos="top-left" @close="showPopup = false">
			<view class="popup_content">
				<u-navbar title="修改名称" bgColor="#ffffff" :titleStyle="{color:'#333'}" leftIconColor="#333"
					:autoBack="false" leftIconSize="40" @leftClick="showPopup = false">
				</u-navbar>
				<u--form labelPosition="left" :model="modify_form" ref="modifyForm" labelWidth="100rpx"
					:labelStyle="{fontSize:'28rpx',color:'#000000'}">
					<u-form-item label="名称" prop="name">
						<u--input v-model="modify_form.name" border="none" :clearable="true"></u--input>
					</u-form-item>
				</u--form>
				<u-button class="big_btn" text="确定" @click="submit">
				</u-button>
			</view>
		</u-popup>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				avatar: [],
				showPopup: false,
				modify_form: {
					name: ''
				},
				userInfo: {},
			};
		},
		async onShow() {
			let storage = JSON.parse(uni.getStorageSync('userInfo'))
			if (storage) {
				this.userInfo = storage;
			}else {
				this.userInfo = await this.$api.getUserInfo({
					phone: storage.phone?storage.phone:0
				});
			}
			this.modify_form.name = this.userInfo.userName;
		},
		methods: {
			async afterRead(event) {
				uni.uploadFile({
					url: '', //输入你的bucketname.endpoint
					filePath: event.file[0].url, //文件路径可以是临时路径 blob
					fileType: 'image',
					name: 'file',
					formData: {
						name: new Date().getTime(),
						key: new Date().getTime(),
						policy: Oss.policy, // policy
						OSSAccessKeyId: Oss.OSSAccessKeyId, // 输入你的AccessKeyId
						success_action_status: '200',
						signature: Oss.signature // signature
					},
					success: res => {
						console.log(res)
					}
				})
			},
			showNamePopup() {
				this.showPopup = true;
			},
			submit() {
				if (!this.modify_form.name) {
					this.$refs.uToast.show({
						type: "error",
						icon: false,
						title: "失败",
						message: "请输入名称",
					});
					return;
				}
				let userInfo = JSON.parse(JSON.stringify(this.userInfo));				
				userInfo.userName = this.modify_form.name;
				this.$u.api.modifyUserInfo({
						userId: userInfo.userId,
						...userInfo
					})
					.then(res => {
						this.getUserInfo();
						this.showPopup = false;
					})
			},
			async getUserInfo() {
				let res = await this.$u.api.getUserInfo({
					phone: this.userInfo.phone
				})
				this.userInfo = res.data;
				uni.setStorageSync('userInfo', JSON.stringify(res.data));
			},
		}
	}
</script>

<style lang="scss">
	.content {
		padding: 0;
		padding-top: 108rpx;
		background-color: #fff;
	}

	.big_btn {
		margin-top: 60rpx;
		width: calc(100vw - 80rpx);
		margin-left: 40rpx;
		background: #333333;
		border: 2rpx solid #333333;
		height: 80rpx;
		font-size: 28rpx;
		color: #ffffff;
		height: 80rpx;
		border-radius: 20rpx;
	}

	.value {
		max-width: 450rpx;
		font-size: 28rpx;
		color: #333 !important;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}

	/deep/ .u-cell__body {
		padding: 40rpx;
		background-color: #fff
	}

	/deep/ .u-line {
		// border-color: #242748 !important;
	}

	/deep/ .u-cell--clickable {
		background: transparent;
	}

	/deep/ .u-upload__wrap {
		flex-direction: inherit;
		flex-wrap: inherit;
	}
	/deep/ .uni-input-input{
		color: #333 !important;
	}
	.popup_content {
		width: 100vw;
		height: 100vh;
		background-color: #f9f9f9;
		padding: 240rpx 0 0;
		color: #333;
		position: relative;
	}

	/deep/.u-form {
		background-color: #ffffff;
		padding: 0 40rpx;
		color: #333;
		font-size: 28rpx;
	}

	/deep/ .u-form-item__body {
		padding: 40rpx 0;
	}
</style>
